package UI;

import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.ImageIcon;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextField;

import COMP.ImageComponent;
import COMP.imgBtn;

public class PopUpTableEdit extends JDialog{
	ImageComponent bg;
	ImageComponent headerImage;
	int[] colSize;
	String[] data;
	boolean[] editable;
	JTextField[] tf;
	int HEADER_HEIGHT = 32;
	private imgBtn btnSave = new imgBtn("Img/butt_save.png");
	private imgBtn btnClose = new imgBtn("Img/butt_close.png");
	
	public PopUpTableEdit(JFrame superclass){
		super(superclass);
		setSize(440, 165);
		this.setBackground(Color.white);
		setLocationRelativeTo(null);
		this.setResizable(false);
		getContentPane().setLayout(null);
		if (bg != null){
		}
		else{
			bg = new ImageComponent(new ImageIcon("Img/.png").getImage());
		}
		bg.setBounds(0,0,440,165);
		bg.setLayout(null);
		getContentPane().add(bg);
		addBtn();
	}
	public void addHeader(){
		if(headerImage!=null){
			headerImage.setBounds(10,30,410,HEADER_HEIGHT);
			bg.add(headerImage);
		}
	}
	public void addTextField(){
		if(colSize != null && data != null){
			int colCount = colSize.length;
			tf = new JTextField[colCount];
			int xInit = 0;
			for(int i = 0;i<colCount-1;i++){
				if (data[i].equals("null")){
					tf[i] = new JTextField();
				}
				else{
					tf[i] = new JTextField(data[i]);					
				}
				tf[i].setBounds(10+xInit,31+HEADER_HEIGHT,colSize[i],30);
				xInit += colSize[i];
				bg.add(tf[i]);
			}
		}
	}
	public void addBtn(){
		btnSave.setBounds(350,100,28,28);
		bg.add(btnSave);
		btnClose.setBounds(380,100,28,28);
		bg.add(btnClose);
		this.eventCloseBtn();
		
	}
	public void setHeaderImage(ImageComponent headerImage){
		this.headerImage = headerImage;
	}
	public void setColSize(int[] colSize){
		this.colSize = colSize;
	}
	public void setData(String[] data){
		this.data = data;
	}
	public void eventSaveBtn(ActionListener e){
		btnSave.addActionListener(e);
	}
	public void eventCloseBtn(){
		btnClose.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				closePopUp();
			}
		});
	}
	public String[] getData(){
		int colCount = tf.length;
		String[] temp = new String[colCount];
		for(int i = 0;i<colCount-1;i++){
			temp[i] = new String(tf[i].getText());
		}
		return temp;
	}
	public void closePopUp(){
		this.dispose();
	}
	public void setTextFieldEditable(boolean[] editable){
		this.editable = editable;
		this.setEditable();
	}
	public void setEditable(){
		int colCount = tf.length;
		for(int i = 0;i<colCount-1;i++){
			tf[i].setEditable(editable[i]);
			if(!editable[i]){
				tf[i].setBackground(Color.LIGHT_GRAY);
			}
		}
	}
}
